﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DocPlat.Library;

namespace DocPlat.UI
{
    public partial class Classifiers : UserControl
    {
        ServerObject mServerObject = null;
        int mTypeID = 0;

        public int TypeID
        {
            get { return mTypeID; }
            set { mTypeID = value; }
        }

        public Classifiers()
        {
            InitializeComponent();
            mServerObject = new ServerObject();
        }

        private void addGrupButton_Click(object sender, EventArgs e)
        {
            ClassifiersGrupInputPanel AddForm = new ClassifiersGrupInputPanel();
            AddForm.CreateNew = true;
            System.Windows.Forms.DialogResult result = AddForm.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("Data was saved");
            }
            else
                if (result == System.Windows.Forms.DialogResult.Cancel)
                {
                    MessageBox.Show("Sorry, data was not saved");
                }
            FillGrupDatagrid();
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            mServerObject.CloseThisTab(this);
        }    

        private void addClassifierbutton_Click(object sender, EventArgs e)
        {
            ClassifiersInputPanel AddForm = new ClassifiersInputPanel();
            AddForm.CreateNew = true;
            AddForm.TypeID = mTypeID;
            System.Windows.Forms.DialogResult result = AddForm.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("Data was saved");
            }
            else
                if (result != System.Windows.Forms.DialogResult.Cancel)
                {
                    MessageBox.Show("Sorry, data was not saved");
                }
            FillClassifiersDataGrid(mTypeID);
        }

        private void Classifiers_Load(object sender, EventArgs e)
        {
            FillGrupDatagrid();
            FillClassifiersDataGrid(mTypeID);
            systemClassifiersCheckBox.Checked = false;
        }

        private void FillGrupDatagrid()
        {
            bool isDelete = false;
            DataTable grupTable = mServerObject.SelectTypeClassifiers(isDelete,systemClassifiersCheckBox.Checked);

            grpoupTreeView.Nodes.Clear();

            // Create the RootNode
            TreeNode rootNode = grpoupTreeView.Nodes.Add("Classifiers Types");
            rootNode.ImageIndex = 0;

            if (grupTable != null)
            {
                for (int i = 0; i < grupTable.Rows.Count; i++)
                {
                    TreeNode states1 = rootNode.Nodes.Add(grupTable.Rows[i]["Name"].ToString());
                    states1.ImageIndex = 1;
                    states1.Tag = grupTable.Rows[i]["TypeID"].ToString();
                }
            }

            grpoupTreeView.ExpandAll();
        }

        private void FillClassifiersDataGrid(int typeID)
        {
            DataTable classifiersTable = mServerObject.SelectClassifiers(typeID, false, systemClassifiersCheckBox.Checked);
            dataGridView2.DataSource = classifiersTable;
        }

        private void dataGridView2_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            ClassifiersInputPanel updateForm = new ClassifiersInputPanel();
            if (e.RowIndex >= 0)
            {
                int codeId = int.Parse(dataGridView2.Rows[e.RowIndex].Cells[0].Value.ToString());
                string nameClassifier = dataGridView2.Rows[e.RowIndex].Cells[1].Value.ToString();
                string description = dataGridView2.Rows[e.RowIndex].Cells[2].Value.ToString();

                updateForm.CodeID = codeId;
                updateForm.NameClassifier = nameClassifier;
                updateForm.Description = description;
                updateForm.TypeID = mTypeID;
                updateForm.CreateNew = false;

                try
                {
                    System.Windows.Forms.DialogResult result = updateForm.ShowDialog();

                    if (result == System.Windows.Forms.DialogResult.OK)
                    {

                        MessageBox.Show("Data was saved");
                    }

                    else
                    {
                        if (result != System.Windows.Forms.DialogResult.Cancel)
                        MessageBox.Show("Data was NOT saved");
                    }
                }
                finally
                {
                    FillClassifiersDataGrid(mTypeID);
                }
            }
        }

        private void grpoupTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode selNod = grpoupTreeView.SelectedNode;
            if (selNod != null && selNod.Tag != null)
            {
                string stringTypeID = selNod.Tag.ToString();

                int typeId = 0;
                int.TryParse(stringTypeID, out typeId);
                mTypeID = typeId;
                FillClassifiersDataGrid(mTypeID);
            }
        }

        private void grpoupTreeView_DoubleClick(object sender, EventArgs e)
        {
            ClassifiersGrupInputPanel typeInputPanel = new ClassifiersGrupInputPanel();
            TreeNode selNod = grpoupTreeView.SelectedNode;

            if (selNod != null && selNod.Tag != null)
            {
                int typeId = 0;
                int.TryParse(selNod.Tag.ToString(), out typeId);
                string nameType = selNod.Text;

                typeInputPanel.TypeID = typeId;
                typeInputPanel.NameType = nameType;
                typeInputPanel.CreateNew = false;

                try
                {
                    System.Windows.Forms.DialogResult result = typeInputPanel.ShowDialog();

                    if (result == System.Windows.Forms.DialogResult.OK)
                    {
                        MessageBox.Show("Data was saved");
                    }

                    else
                    {
                        MessageBox.Show("Data was NOT saved");
                    }
                }
                finally
                {
                    FillGrupDatagrid();
                }
            }
        }

        private void systemClassifiersCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            FillGrupDatagrid();
            FillClassifiersDataGrid(mTypeID);
        }   






    }
}
